import re

from spider.html_downloader import HtmlDownloader
from spider.product import Product


class HtmlParser:
    item_pattern = r'<li class="brick-item brick-item-m brick-item-m-2">([\s\S]*?)</li>'
    title_pattern = r'<h3 class="title"><a href="javascript:;">([\s\S]*?)</a></h3>'
    desc_pattern = r'<p class="desc">([\s\S]*?)</p>'
    price_pattern = r'<span class="num">([\s\S]*?)</span>'

    def parser(self, html):
        """
        解析给定的html
        @param html:
        @return : product
        """
        data = re.findall(self.item_pattern, html)
        result = set()
        for str in data:
            title = re.findall(self.title_pattern, str)
            desc = re.findall(self.desc_pattern, str)
            price = re.findall(self.price_pattern, str)
            result.add(Product(title[0], desc[0], price[0]))
        return result


# downloader = HtmlDownloader()
# html = downloader.download("http://127.0.0.1:8848/xiaomi-master/index.html")
# parser = HtmlParser()
# result = parser.parser(html)
# for i in result:
#     print(i)
